
const loginParams = {
    companySerialNo:1,
    account: '18958061117',
    password: '123456'
}

let getParams = (data)=> {
    return Object.keys(data).map(function(k) {
        return encodeURIComponent(k) + '=' + encodeURIComponent(data[k])
    }).join('&');
}

let login = (params = loginParams, cb)=> {
    fetch('/security/login1', {
        method: 'POST',
        mode: 'cros', //跨域设置
        body: getParams(params),
        headers: {
            'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    })
        .then(res => {
            if (res.ok) {
                return res.json();
            }
            alert(JSON.stringify(res));
        })
        .then(result => {
            localStorage.setItem('userinfo', JSON.stringify(result.data.detail));
    
            fetch('/sysSetting/view', {
                method: 'GET',
                mode: 'cros'
            })
                .then(res => {
                    if (res.ok) {
                        return res.json();
                    }
                    alert(JSON.stringify(res));
                })
                .then(result => {
                    localStorage.setItem('sysSetting', JSON.stringify(result.data));
                    cb && cb();
                });
        })
        .catch(err => {
            console.log(err);
        });
}

export default login;